home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / Sessions / Traut / ZStrings / Source / Win32 / Win32ZString.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  8.5 KB  |  367 lines

  1. /*==================================================================
  2.     File:        Win32ZString.h
  3.     
  4.     Contains:    Windows-specific parsing for ZStrings.
  5.  
  6.     Written by:    Nalini Prakash
  7.     
  8.     Copyright:    2000-2001 Connectix Corporation
  9.     
  10.     This source has been placed into the public domain by
  11.     Connectix Corporation. You have the right to modify, 
  12.     distribute or use this code without any legal limitations
  13.     or finanicial/licensing requirements. Connectix is not 
  14.     liable for any problems that result from the use of this 
  15.     code.
  16.     
  17.     If you have comments, feedback, questions, or would like
  18.     to submit bug fixes or updates to this code, please email
  19.     opensource@connectix.com.
  20. ==================================================================*/
  21.  
  22. #include "Win32ZString.h"
  23. #include "ZStringDictionary.h"
  24.  
  25.  
  26. char *        Win32ZString::sDictionaryData = NULL;
  27.  
  28.  
  29. /*------------------------------------------------------------------
  30.     Initialize                                        [static]
  31.     
  32.     This static method instantiates the singleton classes
  33.     for tracking ZStrings.
  34. ------------------------------------------------------------------*/
  35.  
  36. void
  37. Win32ZString::Initialize()
  38. {
  39.     new ZStringDictionary();
  40.     new Win32ZStringParser();
  41. }
  42.  
  43.  
  44. /*------------------------------------------------------------------
  45.     TearDown                                        [static]
  46. ------------------------------------------------------------------*/
  47.  
  48. void
  49. Win32ZString::TearDown()
  50. {
  51.     delete &ZStringDictionary::GetZStringDictionary();
  52.     delete &ZStringParser::GetZStringParser();
  53. }
  54.  
  55.  
  56. /*------------------------------------------------------------------
  57.     GetTagReplacement
  58. ------------------------------------------------------------------*/
  59.  
  60. Z_UInt16
  61. Win32ZStringParser::GetTagReplacement(
  62.     ZStringTagID                inTagID,
  63.     char *                        outReplacement)
  64. {
  65.     const char *    ellipse = "...";
  66.     const char *    trademark = "(tm)";
  67.     const char *    registeredTrademark = "(r)";
  68.     const char *    copyright = "(c)";
  69.     const char *    mdash = "-";
  70.     const char *    carriageReturn = "\r\n";
  71.  
  72.     const char *    replacementStr = NULL;
  73.     Boolean            specialChar = ((inTagID == kZTag_hellip) || (inTagID == kZTag_trade) ||
  74.                                     (inTagID == kZTag_copy) || (inTagID == kZTag_reg) ||
  75.                                     (inTagID == kZTag_mdash) || (inTagID == kZTag_br));
  76.  
  77.     // By default, all tags are one character.
  78.     UInt16        tagSize = 1;
  79.     
  80.     if (outReplacement != NULL)
  81.     {
  82.         static const char    sTagReplacements[] = {
  83.             '\0',                // kTagIDInvalid
  84.             '\xC6',                // kZTag_AElig
  85.             '\xC1',                // kZTag_Aacute
  86.             '\xC2',                // kZTag_Acirc
  87.             '\xC0',                // kZTag_Agrave
  88.             '\xC5',                // kZTag_Aring
  89.             '\xC3',                // kZTag_Atilde
  90.             '\xC4',                // kZTag_Auml
  91.             '\xC7',                // kZTag_Ccedil
  92.             '\xC9',                // kZTag_Eacute
  93.             '\xCA',                // kZTag_Ecirc
  94.             '\xC8',                // kZTag_Egrave
  95.             '\xCB',                // kZTag_Euml
  96.             '\xCD',                // kZTag_Iacute
  97.             '\xCE',                // kZTag_Icirc
  98.             '\xCC',                // kZTag_Igrave
  99.             '\xCF',                // kZTag_Iuml
  100.             '\xD1',                // kZTag_Ntilde
  101.             '\xD3',                // kZTag_Oacute
  102.             '\xD4',                // kZTag_Ocirc
  103.             '\xD2',                // kZTag_Ograve
  104.             '\xD8',                // kZTag_Oslash
  105.             '\xD5',                // kZTag_Otilde
  106.             '\xD6',                // kZTag_Ouml
  107.             '\xDA',                // kZTag_Uacute
  108.             '\xDB',                // kZTag_Ucirc
  109.             '\xD9',                // kZTag_Ugrave
  110.             '\xDC',                // kZTag_Uuml
  111.             '\xE1',                // kZTag_aacute
  112.             '\xE2',                // kZTag_acirc
  113.             '\xE6',                // kZTag_aelig
  114.             '\xE0',                // kZTag_agrave
  115.             '\x26',                // kZTag_amp
  116.             '\xE5',                // kZTag_aring
  117.             '\xE3',                // kZTag_atilde
  118.             '\xE4',                // kZTag_auml
  119.             '\x84',                // kZTag_bdquo
  120.             '\xB7',                // kZTag_bull
  121.             '\xE7',                // kZTag_ccedil
  122.             '\xA2',                // kZTag_cent
  123.             '\xA9',                // kZTag_copy
  124.             '\xE9',                // kZTag_eacute
  125.             '\xEA',                // kZTag_ecirc
  126.             '\xE8',                // kZTag_egrave
  127.             '\xEB',                // kZTag_euml
  128.             '\x3E',                // kZTag_gt
  129.             '\x85',                // kZTag_hellip
  130.             '\xED',                // kZTag_iacute
  131.             '\xEE',                // kZTag_icirc
  132.             '\xA1',                // kZTag_iexcl
  133.             '\xEC',                // kZTag_igrave
  134.             '\xBF',                // kZTag_iquest
  135.             '\xEF',                // kZTag_iuml
  136.             '"',    //'\x93',    // kZTag_ldquo
  137.             '\'',    //'\x91',    // kZTag_lsquo
  138.             '\x3C',                // kZTag_lt
  139.             '\x97',                // kZTag_mdash
  140.             '\xB5',                // kZTag_micro
  141.             '\xA0',                // kZTag_nbsp
  142.             '\x96',                // kZTag_ndash
  143.             '\xF1',                // kZTag_ntilde
  144.             '\xF3',                // kZTag_oacute
  145.             '\xF4',                // kZTag_ocirc
  146.             '\xF2',                // kZTag_ograve
  147.             '\xF8',                // kZTag_oslash
  148.             '\xF5',                // kZTag_otilde
  149.             '\xF6',                // kZTag_ouml
  150.             '\xB6',                // kZTag_para
  151.             '\xE3',                // kZTag_pi            !!!
  152.             '\xA3',                // kZTag_pound
  153.             '"',    //'\x94',    // kZTag_rdquo
  154.             '\xAE',                // kZTag_reg
  155.             '\x00',                // kZTag_replace
  156.             '\'',    //'\x92',    // kZTag_rsquo
  157.             '\x82',                // kZTag_sbquo
  158.             '\xDF',                // kZTag_szlig
  159.             '\x99',                // kZTag_trade
  160.             '\xFA',                // kZTag_uacute
  161.             '\xFB',                // kZTag_ucirc
  162.             '\xF9',                // kZTag_ugrave
  163.             '\xFC',                // kZTag_uuml
  164.             '\xA5',                // kZTag_yen
  165.             '\xFF',                // kZTag_yuml
  166.             '\x0D'                // kZTag_br
  167.         };
  168.  
  169.         check(inTagID > kZTag_Invalid && inTagID < sizeof(sTagReplacements)/sizeof(char));
  170.         check(inTagID != kZTag_replace);
  171.  
  172.         // fill in the equivalent    
  173.         *outReplacement = sTagReplacements[inTagID];
  174.  
  175.     }
  176.  
  177.     switch (inTagID)
  178.     {
  179.         case kZTag_hellip:
  180.             replacementStr = ellipse;
  181.             break;
  182.  
  183.         case kZTag_trade:
  184.             replacementStr = trademark;
  185.             break;
  186.  
  187.         case kZTag_copy:
  188.             replacementStr = copyright;
  189.             break;
  190.  
  191.         case kZTag_reg:
  192.             replacementStr = registeredTrademark;
  193.             break;
  194.             
  195.         case kZTag_mdash:
  196.             replacementStr = mdash;
  197.             break;
  198.         
  199.         case kZTag_br:
  200.             replacementStr = carriageReturn;
  201.             break;
  202.         
  203.         default:
  204.             break;
  205.     }
  206.  
  207.     if (specialChar)
  208.     {
  209.         if (outReplacement)
  210.             strcpy(outReplacement, replacementStr);
  211.         tagSize = strlen(replacementStr);
  212.     }
  213.  
  214.     return tagSize;
  215. }
  216.  
  217.  
  218. /*------------------------------------------------------------------
  219.     LoadOverrideDictionary
  220. ------------------------------------------------------------------*/
  221.  
  222. Z_Boolean
  223. Win32ZString::LoadOverrideDictionary(
  224.     UInt16            inOverrideID)
  225. {
  226.     HRSRC             resource;
  227.     HGLOBAL         resHandle;
  228.     void *            resData;
  229.     UInt32             resSize;
  230.  
  231.     // find the resource
  232.     resource = ::FindResource(NULL, (LPCTSTR)inOverrideID, "DICT");
  233.     if (resource == NULL)
  234.         return false;
  235.     
  236.     // get the size
  237.     resSize = ::SizeofResource(NULL, resource);
  238.     if (resSize == 0)
  239.         goto CantSizeResource;
  240.     
  241.     // load the resource
  242.     resHandle = ::LoadResource(NULL, resource);
  243.     if (resHandle == NULL)
  244.         goto CantLoadResource;
  245.     
  246.     // get the data
  247.     resData = ::LockResource(resHandle);
  248.     if (resData == NULL)
  249.         goto CantLockResource;
  250.     
  251.     // delete any old dictionary
  252.     delete[] sDictionaryData;
  253.  
  254.     // allocate memory
  255.     sDictionaryData = new char[resSize + 2];
  256.  
  257.     if (sDictionaryData == NULL)
  258.         goto CantAllocateMemory;
  259.     ::memset(sDictionaryData, 0, resSize + 2);
  260.     ::memcpy(sDictionaryData, resData, resSize);
  261.  
  262.     // populate the dictionary
  263.     ZString::PopulateDictionary(sDictionaryData);
  264.     return true;
  265.  
  266. CantAllocateMemory:
  267. CantLockResource:
  268. CantLoadResource:
  269. CantSizeResource:
  270.     return false;
  271. }
  272.  
  273.  
  274. /*------------------------------------------------------------------
  275.     LoadOverrideDictionary
  276. ------------------------------------------------------------------*/
  277.  
  278. Z_Boolean
  279. Win32ZString::LoadOverrideDictionary(
  280.     const char *    inFilename)
  281. {
  282.     FILE *            file;
  283.     UInt32             fileSize;
  284.  
  285.     // open the file
  286.     file = ::fopen(inFilename, "rb");
  287.     if (file == NULL)
  288.         return false;
  289.     
  290.     // determine the size
  291.     ::fseek(file, 0, SEEK_END);
  292.     fileSize = ::ftell(file);
  293.     ::fseek(file, 0, SEEK_SET);
  294.     
  295.     // delete any old dictionary
  296.     delete[] sDictionaryData;
  297.  
  298.     // allocate memory
  299.     sDictionaryData = new char[fileSize + 2];
  300.  
  301.     if (sDictionaryData == NULL)
  302.         goto CantAllocateMemory;
  303.     ::memset(sDictionaryData, 0, fileSize + 2);
  304.     ::fread(sDictionaryData, 1, fileSize, file);
  305.  
  306.     // populate the dictionary
  307.     ZString::PopulateDictionary(sDictionaryData);
  308.     return true;
  309.  
  310. CantAllocateMemory:
  311.     return false;
  312. }
  313.  
  314.  
  315. /*------------------------------------------------------------------
  316.     CalcOverrideDictionaryID
  317. ------------------------------------------------------------------*/
  318.  
  319. Z_UInt16
  320. Win32ZString::CalcOverrideDictionaryID()
  321. {
  322.     Z_UInt16    dictionaryID = kEnglishOverrideID;
  323.  
  324.     // Get the calling thread's current locale. 
  325.     LCID lcid = GetThreadLocale();
  326.  
  327.     // Get the primary language.
  328.     WORD languageID = LANGIDFROMLCID(lcid);
  329.     WORD primaryLanguage = PRIMARYLANGID(languageID);
  330.  
  331.     switch (primaryLanguage)
  332.     {
  333.         default:
  334.         case LANG_ENGLISH:
  335.             dictionaryID = kEnglishOverrideID;
  336.             break;
  337.  
  338.         case LANG_FRENCH:
  339.             dictionaryID = kFrenchOverrideID;
  340.             break;
  341.  
  342.         case LANG_GERMAN:
  343.             dictionaryID = kGermanOverrideID;
  344.             break;
  345.  
  346.         case LANG_ITALIAN:
  347.             dictionaryID = kItalianOverrideID;
  348.             break;
  349.  
  350.         case LANG_SPANISH:
  351.             dictionaryID = kSpanishOverrideID;
  352.             break;
  353.  
  354.         case LANG_JAPANESE:
  355.             dictionaryID = kJapaneseOverrideID;
  356.             break;
  357.  
  358.         case LANG_PORTUGUESE:
  359.             dictionaryID = kPortugueseOverrideID;
  360.             break;
  361.     }
  362.     
  363.     return dictionaryID;
  364. }
  365.  
  366.  
  367.